package com.xie.controller;

import com.xie.pojo.Dog;
import com.xie.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dog")
public class DogController {

    @Autowired
    @Qualifier("DogServiceImpl")
    private DogService dogService;

    //查询所有宠物
    @RequestMapping("/allDog")
    public String allDog(Model model){
        List<Dog> list = dogService.allDog();
        model.addAttribute("list",list);
        return "welcome";
    }
    @RequestMapping("/allDog2")
    public String allDog2(Model model){
        List<Dog> list = dogService.allDog();
        model.addAttribute("list",list);
        return "welcomeuser";
    }
    //跳转添加页面
    @RequestMapping("/toadd")
    public String toAdd(){
        return "addDog";
    }
    //添加
    @RequestMapping("/addDog")
    public String addDog(Dog dog){
        System.out.println("dog=>" + dog);
        dogService.addDog(dog);
        return "redirect:/dog/allDog";
    }
    //删除宠物
    @RequestMapping("/deleteDog")
    public String deleteDog(@RequestParam("id") int id){
        dogService.deleteDog(id);
        return "redirect:/dog/allDog";
    }
    //查询某个宠物
    @RequestMapping("/queryDogByText")
    public String queryDogByText(@RequestParam("dtext") String dtext,Model model){
        Dog dog = dogService.queryDogByText(dtext);
        System.out.println(dog);
        List<Dog> list = new ArrayList<Dog>();
        list.add(dog);
        if(dog == null){
            list = dogService.allDog();
            model.addAttribute("error","没查到");
        }
        model.addAttribute("list",list);
        return "welcome";
    }

}
